package com.ruoyi.market.service.warning;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.market.config.AliyunConfig;
import com.ruoyi.market.constant.SystemConstant;
import com.ruoyi.market.model.param.WarnInfoParam;
import com.ruoyi.market.model.result.WarnInfoResult;
import com.ruoyi.market.service.IWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AliAccountBalanceWarningService implements IWarningService {

    @Autowired
    AliyunConfig aliyunConfig;

    @Override
    public WarnInfoResult handleWarnInfo(WarnInfoParam warnInfoParam) {
        WarnInfoResult warnInfoResult = new WarnInfoResult();
        warnInfoResult.setTitle("阿里云余额");
        try {

            if (!aliyunConfig.isEnabled()){
                warnInfoResult.setMessage("阿里云账户余额查询未开启");
                return warnInfoResult;
            }
            if (StrUtil.hasEmpty(aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret())){
                warnInfoResult.setMessage("阿里云账户余额查询密钥有空值");
                return warnInfoResult;
            }
            com.aliyun.bssopenapi20171214.Client client =
                    createClient(
                            SecureUtil.aes(StrUtil.bytes(SystemConstant.AES_KEY)).decryptStr(aliyunConfig.getAccessKeyId()),
                            SecureUtil.aes(StrUtil.bytes(SystemConstant.AES_KEY)).decryptStr(aliyunConfig.getAccessKeySecret())
                    );
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            com.aliyun.bssopenapi20171214.models.QueryAccountBalanceResponse resp = client.queryAccountBalanceWithOptions(runtime);

            if (resp.getStatusCode() == 200 && resp.getBody().success){
                String availableAmount = resp.getBody().getData().getAvailableAmount();
                warnInfoResult.setMessage(availableAmount);
                return warnInfoResult;
            }
            warnInfoResult.setMessage(resp.getBody().getCode() + "|" + resp.getBody().getMessage());

        }catch (Exception exception){
            exception.printStackTrace();
            warnInfoResult.setMessage(exception.getMessage());
        }
        return warnInfoResult;
    }


    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.bssopenapi20171214.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "business.aliyuncs.com";
        return new com.aliyun.bssopenapi20171214.Client(config);
    }

    /**
     * 使用STS鉴权方式初始化账号Client，推荐此方式。本示例默认使用AK&SK方式。
     * @param accessKeyId
     * @param accessKeySecret
     * @param securityToken
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.bssopenapi20171214.Client createClientWithSTS(String accessKeyId, String accessKeySecret, String securityToken) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret)
                // 必填，您的 Security Token
                .setSecurityToken(securityToken)
                // 必填，表明使用 STS 方式
                .setType("sts");
        // 访问的域名
        config.endpoint = "business.aliyuncs.com";
        return new com.aliyun.bssopenapi20171214.Client(config);
    }

}
